﻿using AuthCenter_HuangJingtao.Domain;
using AuthCenter_HuangJingtao.ErrorCode;
using AuthCenter_HuangJingtao.Infrastructure.Imterfaces;
using AuthCenter_HuangJingtao.Read.Api.Application.Command.AppaLL;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace AuthCenter_HuangJingtao.Read.Api.Application.CommandHandler.AppHandler
{
    public class UpdateCommandHandler : IRequestHandler<UpdateCommand, ApiResult<int>>
    {
        IBaseReponsitory<App> Apps;

        public UpdateCommandHandler(IBaseReponsitory<App> Apps)
        {
            this.Apps = Apps;
        }

        public async Task<ApiResult<int>> Handle(UpdateCommand request, CancellationToken cancellationToken)
        {



            var Tst = Apps.GetQuery().Where(x => x.App_Name == request.Apps.App_Name && x.App_Id != request.Apps.App_Id&&!x.App_IsDel ).ToList();
            if (Tst.Count > 0)
            {
                return new ApiResult<int>
                {
                    Code = ApiEnum.False,
                    Message = "用户名称已存在",
                    Data = -1
                };
            }
            else
            {
                var list = await Apps.GetQuery().FirstOrDefaultAsync(c => c.App_Id == request.Apps.App_Id);
                list.App_Name = request.Apps.App_Name;
                list.App_Sex = request.Apps.App_Sex;
                list.AddressId = request.Apps.AddressId;
                list.CityId = request.Apps.CityId;
                list.XianId = request.Apps.XianId;
                list.App_Discription = request.Apps.App_Discription;
                list.App_People = request.Apps.App_People;
                list.EditPerson = request.Apps.EditPerson;
                list.EditTime = DateTime.Now;
                await Apps.UpdateAsync(list);
                return new ApiResult<int>
                {
                    Code = ApiEnum.Success,
                    Message = "修改成功",
                    Data = 1

                };
            }

        }
    }
}
